package com.example.exam.sysDictInfo.controller;

import com.example.exam.common.ResultBean;
import com.example.exam.common.ResultBeanCodeEnum;
import com.example.exam.db.PageInfoBean;
import com.example.exam.sysDictInfo.model.SysDictInfoBean;
import com.example.exam.sysDictInfo.service.SysDictInfoService;
import com.example.exam.user.model.UserRoleBean;
import com.example.exam.user.service.UserRoleService;
import com.example.exam.utils.StringToList;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/sysDictInfoController")
public class SysDictInfoController {
    String base_path="/sysDictInfo/";
    @Autowired
    SysDictInfoService sdis;
    @Autowired
    UserRoleService userRoleService;
    @GetMapping("/toSysDictInfo")
        /**
            * @Description:去字典列表页面
            * @Param: [request]
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String toSysDictInfo(HttpServletRequest request){
        HttpSession session=request.getSession();
        Integer userId=(Integer) session.getAttribute("id");
        ResultBean resultBean  = new ResultBean();
        List<UserRoleBean> list=userRoleService.authorization(userId);
        if(list!=null){
            for (int i=0;i<list.size();i++){
                if("SysDict|toEdit".equals(list.get(i).getPre())){
                    request.setAttribute("update",list.get(i));
                }else if("SysDict|toDelete".equals(list.get(i).getPre())){
                    request.setAttribute("delete",list.get(i));
                }else if("SysDict|toGrant".equals(list.get(i).getPre())){
                    request.setAttribute("grant",list.get(i));
                }else if ("SysDict|toAdd".equals(list.get(i).getPre())){
                    request.setAttribute("add",list.get(i));
                }
            }
        }
        return base_path+"sysDictInfo";
    }
    @GetMapping("/toAddDict")
        /**
            * @Description:去添加字典信息
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String toAddDict(){
        return base_path+"addDict";
    }
    @GetMapping("/toUpdateDict")
        /**
            * @Description:去编辑字典信息
            * @Param: []
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String toUpdateDict(HttpServletRequest request,Integer id){
        request.setAttribute("id",id);
        return base_path+"UpdateDict";
    }
    @PostMapping("/selectDict")
    @ResponseBody
        /**
            * @Description:字典信息列表
            * @Param: [request, page, limit]
            * @return: java.lang.Object
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public Object list(HttpServletRequest request,@RequestParam("page") Integer page,@RequestParam("limit") Integer limit) {
        String name = request.getParameter("deliveryName");
        ResultBean resultBean = new ResultBean();
        try {
            PageInfoBean pageInfo = sdis.toList(page,limit,name);
            resultBean.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            resultBean.fillData(pageInfo);
            return resultBean;
        } catch (Exception e) {
            e.printStackTrace();
            resultBean.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
            return resultBean;
        }
    }
    @PostMapping("/insertDict")
    @ResponseBody
        /**
            * @Description:添加字典信息
            * @Param: [sdib, request]
            * @return: com.example.exam.common.ResultBean
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public ResultBean insertDict(@ModelAttribute SysDictInfoBean sdib, HttpServletRequest request){
        sdib.setCreate_time(new Date());
        HttpSession session=request.getSession();
        Integer id=(Integer) session.getAttribute("id");
        sdib.setCreate_user(id);
        sdib.setState(1);
        ResultBean result = new ResultBean();
        try {
            Integer i=sdis.insertDict(sdib);
            result.fillData(i);
            if(i == 1) {
                result.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }else {
                result.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            result.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
        }
        return result;
    }
    @PostMapping("/updateDict")
    @ResponseBody
        /**
            * @Description:修改字典信息
            * @Param: [sdib]
            * @return: com.example.exam.common.ResultBean
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public ResultBean updateDict(@ModelAttribute SysDictInfoBean sdib){
        ResultBean result = new ResultBean();
        try {
            System.out.println("pid"+sdib.getPid());
            Integer i=sdis.updateDict(sdib);
            result.fillData(i);
            if(i == 1) {
                result.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }else {
                result.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            result.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
        }
        return result;
    }
    @PostMapping("/deleteDict")
        /**
            * @Description:删除字典信息
            * @Param: [sdib]
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String deleteDict(@ModelAttribute SysDictInfoBean sdib){
        Integer i=sdis.deleteDict(sdib.getId());
        if(i==1){
            return base_path+"sysDictInfo";
        }else{
            return base_path+"deleteDict";
        }
    }
    @GetMapping("/selectOneDict")
    @ResponseBody
        /**
            * @Description:查询一条字典信息记录
            * @Param: [sdib, response]
            * @return: java.lang.String
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public String selectOneDict(Integer id,HttpServletResponse response) throws IOException {
        //System.out.println("id是"+id);
        SysDictInfoBean sdib2=sdis.selectOneDict(id);
        String str=sdib2.getId()+","+sdib2.getPid()+","+sdib2.getName()+","+sdib2.getMark()+","+sdib2.getDescribe();
        response.setCharacterEncoding("UTF-8");
        return str;
    }
    @PostMapping("/findParentDict")
    @ResponseBody
        /**
            * @Description:查询父级字典
            * @Param: []
            * @return: java.util.List<com.example.exam.sysDictInfo.model.SysDictInfoBean>
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public List<SysDictInfoBean> findParentDict(){
        ResultBean resultBean=new ResultBean();
        List<SysDictInfoBean> list=null;
        try{
            list=sdis.findParentDict();
            resultBean.fillData(list);
            if(list != null) {
                resultBean.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                resultBean.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }else {
                resultBean.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                resultBean.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }
        }catch (Exception e){
            e.printStackTrace();
            resultBean.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            resultBean.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
        }
        return list;
    }
    @RequestMapping("/toDelete")
    @ResponseBody
        /**
            * @Description:批量删除字典信息
            * @Param: [request]
            * @return: com.example.exam.common.ResultBean
            * @Author: xhw
            * @Date: 2021/7/26
            */
    public ResultBean toDelete(HttpServletRequest request) {
        ResultBean result = new ResultBean();
        String role_id  = request.getParameter("id");
        try {
            Integer deleState = null;
            if(StringUtils.isNotEmpty(role_id)) {
                int[] roleIds = StringToList.StrToArry(role_id);
                for(int i = 0; i < roleIds.length; i++) {
                    deleState = sdis.deleteDict(roleIds[i]);
                }
                result.fillData(deleState);
                result.setCode(ResultBeanCodeEnum.OPERA_SUCCESS.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_SUCCESS.getMsg());
            }else {
                result.setCode(ResultBeanCodeEnum.OPERA_FAIL.getCode());
                result.setMsg(ResultBeanCodeEnum.OPERA_FAIL.getMsg());
            }
            result.fillData(deleState);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(ResultBeanCodeEnum.OPERA_EXCEPTION.getCode());
            result.setMsg(ResultBeanCodeEnum.OPERA_EXCEPTION.getMsg());
        }
        return result;
    }
}
